<template>
  <div class="myheader" id="header">
    <el-menu
      :default-active="defaultActiveIndex"
      class="el-menu-demo"
      mode="horizontal"
      :router="true"
      @select="handleSelect"
    >
      <el-menu-item index="/manager/basedata/user">
        <i class="el-icon-setting"></i>
        <span slot="title">基础数据</span>
      </el-menu-item>
      <el-menu-item index="/manager/purchase/order">
        <i class="el-icon-menu"></i>
        <span slot="title">资产采购</span>
      </el-menu-item>

      <el-menu-item index="/manager/borrow/borrow_application">
        <i class="el-icon-setting"></i>
        <span slot="title">资产借用</span>
      </el-menu-item>
      <el-menu-item index="/manager/store/detail">
        <i class="el-icon-setting"></i>
        <span slot="title">库存管理</span>
      </el-menu-item>
      <li class="admin">
        当前用户:{{user.username}}
        <el-button type="text" @click="logout" style="font-size:16px; color:#4fefe1">退出</el-button>&emsp; 时间:
        <span class="time">{{time}}</span>
      </li>
    </el-menu>
  
  </div>
</template>
<script>
export default {
  data() {
    return {
      defaultActiveIndex: "",
      user: {},
      time: "",
    };
  },
  created() {
    this.RefreshActiveIndex();
    this.timer();
    this.check_islogined();
  },
  methods: {
    handleSelect(index) {
      this.defaultActiveIndex = index;
      window.console.log(this.defaultActiveIndex);
    },
    timer() {
      let date = new Date();
      this.time = date.toLocaleTimeString();
      setTimeout(this.timer, 1000);
    },
    //查看是否处于已登录状态
    check_islogined() {
      let session = sessionStorage.getItem("user_session_id");
      this.$http.post("login/check", { token: session }).then(response => {
        if (response.data =="no") {
           this.$router.push("/login")
        } else {
           this.user = response.data;
        }
      });
    },

   
    logout() {
      sessionStorage.removeItem("user_session_id");
      location.reload();
    },
    //更新激活状态
    RefreshActiveIndex() {
      let cur_path = this.$route.path;
      cur_path = cur_path.split("/");
      let midpath = cur_path[cur_path.length - 2];
      cur_path.splice(cur_path.length - 1, 1);
      cur_path = cur_path.join("/");
      cur_path += "/";
      if (midpath == "basedata") {
        cur_path += "user";
      } else if (midpath == "borrow") {
        cur_path += "borrow_application";
      } else if (midpath == "purchase") {
        cur_path += "order";
      } else if (midpath == "store") {
        cur_path += "detail";
      }
      this.defaultActiveIndex = cur_path;
    }
  },
  watch: {
    $route: function() {
      // 路由改变时执行
      //console.info("to.path:" + to.path);
      this.RefreshActiveIndex();
    }
  }
};
</script>
<style>
#header .admin {
  /* background-color: red ; */
  float: right;
  outline: none;
}
#header .time {
  /* background-color: red ; */
  border-radius: 2px;
  background-color: rgb(214, 205, 205);
  padding: 2px 10px;
}
</style>